{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Exercise -Version Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- This is the exercise based on the forked repository on GitHub\n",
    "-  Help to improve it if you want, just add the solution under the question\n",
    "- Each notebook only contains 10 questions which is convenient for everyone to locate each question to modify, you can add your own name under the author's list below, let's contribute together !\n",
    "- To visit my personal site: [Click Here](http://yaozeliang.com/resume)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 51\n",
    "> ***Write a function to compute 5/0 and use try/except to catch the exceptions.***\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "don't divide by zero: division by zero\n"
     ]
    }
   ],
   "source": [
    "def divide():\n",
    "    return 5/0\n",
    "try:\n",
    "    divide()\n",
    "except ZeroDivisionError as e:\n",
    "    print(\"don't divide by zero:\",e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 52\n",
    "> ***Define a custom exception class which takes a string message as attribute.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exception: num is greater than 10\n"
     ]
    }
   ],
   "source": [
    "class CustomException(Exception):\n",
    "    def __init__(self,message):\n",
    "        self.message=message\n",
    "\n",
    "num=int(input())\n",
    "\n",
    "try:\n",
    "    if num>=10:\n",
    "        raise CustomException(\"num is greater than 10\")\n",
    "    elif num<10:\n",
    "        raise CustomException(\"num is smaller than 10\")\n",
    "except CustomException as e:\n",
    "    print(\"Exception:\",e.message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 53\n",
    "> ***Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only.***\n",
    "\n",
    "> ***Example:\n",
    "If the following email address is given as input to the \n",
    "program:***\n",
    "```\n",
    "john@google.com\n",
    "```\n",
    "> ***Then, the output of the program should be:***\n",
    "```\n",
    "john\n",
    "```\n",
    "> ***In case of input data being supplied to the question, it should be assumed to be a console input.***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('john', 'google')]\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "email=\"john@google.com\"\n",
    "ans=re.findall(\"(\\w+)@(\\w+).com\",email)\n",
    "print(ans)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 54\n",
    "\n",
    "> ***Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only.***\n",
    "\n",
    "> ***Example:\n",
    "If the following email address is given as input to the program:***\n",
    "```\n",
    "john@google.com\n",
    "```\n",
    "> ***Then, the output of the program should be:***\n",
    "```\n",
    "google\n",
    "```\n",
    "> ***In case of input data being supplied to the question, it should be assumed to be a console input.***\n",
    "\n",
    "### Hints \n",
    "> ***Use \\w to match letters.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('google',)\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "email=\"user@google.com\"\n",
    "regix=re.compile(\"\\w+@(\\w+).com\")\n",
    "output=re.match(regix,email)\n",
    "print(output.groups())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 55\n",
    ">***Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only.***\n",
    "\n",
    ">***Example:\n",
    "If the following words is given as input to the program:***\n",
    "```\n",
    "2 cats and 3 dogs.\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "['2', '3']\n",
    "```\n",
    ">***In case of input data being supplied to the question, it should be assumed to be a console input.***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2', '3']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value=input().split(\" \")\n",
    "res=[]\n",
    "for item in value:\n",
    "    if item.isdigit():\n",
    "        res.append(item)\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2', '3']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "value=input()\n",
    "ans=re.findall(r\"\\d+\",value)\n",
    "print(ans)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 56\n",
    "> ***Print a unicode string \"hello world\".***\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unicode hello world\n"
     ]
    }
   ],
   "source": [
    "print(u\"unicode hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 57\n",
    "> ***Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8.***\n",
    "\n",
    "\n",
    "### Hints \n",
    "> ***Use unicode()/encode() function to convert.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value=input()\n",
    "value.encode('utf-8')\n",
    "value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 58\n",
    "> ***Write a special comment to indicate a Python source code file is in unicode.***\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding:utf-8 -*-"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 59\n",
    ">***Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).***\n",
    "\n",
    ">***Example:\n",
    "If the following n is given as input to the program:***\n",
    "```\n",
    "5\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "3.55\n",
    "```\n",
    ">***In case of input data being supplied to the question, it should be assumed to be a console input.***\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.55:3.6\n"
     ]
    }
   ],
   "source": [
    "n=int(input())\n",
    "res=0\n",
    "for i in range(1,n+1):\n",
    "    res=res+float(i/(i+1))\n",
    "print(\"%.2f:%.1f\"%(res,res))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 60\n",
    "\n",
    ">***Write a program to compute:***\n",
    "```\n",
    "f(n)=f(n-1)+100 when n>0\n",
    "and f(0)=1\n",
    "```\n",
    ">***with a given n input by console (n>0).***\n",
    "\n",
    ">***Example:\n",
    "If the following n is given as input to the program:***\n",
    "```\n",
    "5\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "500\n",
    "```\n",
    ">***In case of input data being supplied to the question, it should be assumed to be a console input.***\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fibonacci_recursive(n):\n",
    "    print(\"Calculating F\", \"(\", n, \")\", sep=\"\", end=\", \")\n",
    "\n",
    "    # Base case\n",
    "    if n == 0:\n",
    "        return 0\n",
    "    elif n == 1:\n",
    "        return 1\n",
    "\n",
    "    # Recursive case\n",
    "    else:\n",
    "        return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
